﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.Admin.Filters;

namespace Unmanned_MSUI.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class OrderDetaIlsController : Controller
    {
        #region 构造函数
        private IOrderDetaIlsBLL _orderDetaIlsBLL;
        private IOrderInfoBLL _orderInfoBLL;
        public OrderDetaIlsController(
            IOrderDetaIlsBLL orderDetaIlsBLL
            , IOrderInfoBLL orderInfoBLL
            )
        {
            _orderDetaIlsBLL = orderDetaIlsBLL;
            _orderInfoBLL = orderInfoBLL;
        }
        #endregion

        #region 显示视图

        //订单详情列表显示
        public IActionResult ListView()
        {
            return View();
        }

        //订单详情编辑
        public IActionResult UpdateOrderDetaIlsView()
        {
            return View();
        }

        public IActionResult CreateOrderDetaIlsView()
        {
            return View();
        }

        #endregion

        #region 订单详情列表获取的接口（GetOrderDetaIls）
        /// <summary>
        /// 获取订单详情列表
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页几条数据</param>
        /// <param name="orderInfoId">订单id</param>
        /// <param name="count">数据总数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOrderDetaIls(int page, int limit, string orderInfoId)
        {
            //总条数
            int count;
            //调用BLL的获取订单详情方法
            List<GetOrderDetaIlsDTO> list = _orderDetaIlsBLL.GetOrderDetaIls(page, limit, orderInfoId, out count);

            //返回结果
            ReturnResult result = new ReturnResult()
            {
                Code = 0,//layui要求状态码为0
                Msg = "获取成功！",
                Data = list,
                IsSuccess = true,
                Count = count
            };
            //返回结果
            return Json(result);
        }
        #endregion

        #region 新增订单详情的接口（CreateOrderDetaIls）
        [HttpPost]
        public IActionResult CreateOrderDetaIls([FromForm] OrderDetaIls orderDetaIl)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用添加部门的方法
            string msg;
            result.IsSuccess = _orderDetaIlsBLL.CreateOrderDetaIls(orderDetaIl, out msg);
            result.Msg = msg;
            //判断是否添加成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);
        }
        #endregion

        #region 更新修改订单详情的接口（UpdateOrderDetaIls）

        [HttpPost]
        public IActionResult UpdateOrderDetaIls([FromForm] OrderDetaIls orderDetaIls)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用方法
            string msg;
            bool isOk = _orderDetaIlsBLL.UpdateOrderDetaIls(orderDetaIls, out msg);
            result.Msg = msg;//把返回的描述
            //判断是否成功
            if (isOk)
            {
                result.Code = 200;
                result.IsSuccess = isOk;
            }
            return Json(result);//返回结果
        }

        #endregion

        #region 订单详情软删除 （DeleteOrderDetaIl）
        public IActionResult DeleteOrderDetaIl(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "菜单Id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOk = _orderDetaIlsBLL.DeleteOrderDetaIls(id);
            //判断删除情况
            if (isOk)
            {
                result.Msg = "删除订单详情成功！";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }
        #endregion

        #region 订单详情批量软删除的接口（DeleteOrderDetaIlss）
        /// <summary>
        /// 批量员工软删除的接口
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteOrderDetaIlss(List<string> ids)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (ids == null || ids.Count == 0)
            {
                result.Msg = "你还没有选择要删除的员工！";
                return Json(result);
            }
            //批量删除的方法
            bool isOk = _orderDetaIlsBLL.DeleteOrderDetaIlss(ids);
            //判断是否成功
            if (isOk)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            else
            {
                result.Msg = "删除失败！";
            }
            //返回结果
            return Json(result);
        }

        #endregion
    }
}
